<?php
/**
 * 后台首页控制器
 *
 */

namespace app\admin\controller;

use app\admin\model\AdminLog;
use app\admin\model\AdminMenu;
use app\admin\model\AdminRole;
use app\admin\model\AdminUser;
use app\common\model\Attendance;
use app\common\model\BasicSalary;
use app\common\model\Commission;
use app\common\model\EmailLog;
use app\common\model\Performance;
use app\common\model\SalaryLog;
use app\common\model\Staff;
use tools\SystemInfo;
use think\Request;
use function GuzzleHttp\Psr7\str;

class IndexController extends Controller
{
    public function index(Request $request)
    {

        //默认密码修改检测
        $password_danger = 0;
        if (config('admin.password_warning') && $this->user->id == 1 && password_verify('super-admin', $this->user->password)) {
            $password_danger = 1;
        }

        /**
         * 首页数据展示，可自行替换
         */
        //上月时间
        $time = strtotime(date('Y-m-01') . " - 1 month");

        $this->assign([
            //员工数量
            'staff_count'  => Staff::count(),
            //工资基数数量
            'basic_staff_count' => BasicSalary::count(),
            //工资总数
            'salary_total_count' =>SalaryLog::count(),
            //考勤总数量
            "attendance_total_count" => Attendance::count(),
            //绩效总数量
            "performance_total_count" => Performance::count(),
            //提成总数量
            "commission_total_count" => Commission::count(),
            //邮件总数量
            "email_total_count" => EmailLog::count(),

            'salary_count' =>SalaryLog::where('salary_time',$time)->count(),
            //邮件总数量
            "email_count" => EmailLog::where('salary_time',$time)->count(),
            //考勤前一个月数量
            "attendance_count" => Attendance::where('attendance_time',$time)->count(),
            //绩效前一个月数量
            "performance_count" => Performance::where('performance_time',$time)->count(),
            //提成前一个月数量
            "commission_count" => Commission::where('commission_time',$time)->count(),
        ]);

        //是否显示欢迎信息
        $welcome_info = config('admin.welcome_info');
        if ($welcome_info) {
            $this->admin['name'] = config('admin.name');
        }

        $this->assign([
            //系统信息
            'system_info'     => SystemInfo::getSystemInfo(),
            //访问信息
            'visitor_info'    => $request,
            //默认密码警告
            'password_danger' => $password_danger,
            //当前用户
            'user'            => $this->user,
            //欢迎信息
            'welcome_info'    => $welcome_info
        ]);
        return $this->fetch();
    }
    public function getinfo(Request $request)
    {
        $timestr = $request->param('salary_time');
        $time = strtotime($timestr);
        $arr = [
            'salary_count' =>SalaryLog::where('salary_time',$time)->count(),
            //邮件总数量
            "email_count" => EmailLog::where('salary_time',$time)->count(),
            //考勤前一个月数量
            "attendance_count" => Attendance::where('attendance_time',$time)->count(),
            //绩效前一个月数量
            "performance_count" => Performance::where('performance_time',$time)->count(),
            //提成前一个月数量
            "commission_count" => Commission::where('commission_time',$time)->count()
        ];
        success('success','',$arr);
    }
}
